package com.qing.picture.controller;

import cn.hutool.core.util.ObjectUtil;
import com.qing.picture.common.BaseResponse;
import com.qing.picture.common.ResultUtils;
import com.qing.picture.exception.ErrorCode;
import com.qing.picture.exception.ThrowUtils;
import com.qing.picture.model.dto.spaceUser.SpaceUserAddRequest;
import com.qing.picture.model.dto.spaceUser.SpaceUserEditRequest;
import com.qing.picture.model.dto.spaceUser.SpaceUserQueryRequest;
import com.qing.picture.model.entity.SpaceUser;
import com.qing.picture.model.entity.User;
import com.qing.picture.model.vo.SpaceUserVo;
import com.qing.picture.service.SpaceUserService;
import com.qing.picture.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/spaceUser")
public class SpaceUserController {
    @Resource
    private SpaceUserService spaceUserService;

    @Resource
    private UserService userService;

    /**
     * 添加空间用户
     * @param spaceUser
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addSpaceUser(@RequestBody SpaceUserAddRequest spaceUser, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUser == null, ErrorCode.PARAMS_ERROR);
        Long userId = spaceUserService.addSpaceUser(spaceUser);
        return ResultUtils.success(userId);
    }

    /**
     * 删除空间用户
     * @param sUserId
     * @param request
     * @return
     */
    @GetMapping("/delete")
    public BaseResponse<Boolean> delSpaceUser(@RequestParam("sUserId") Long sUserId, HttpServletRequest request){
        ThrowUtils.throwIf(sUserId == null|| sUserId <= 0, ErrorCode.PARAMS_ERROR);

        // 判断是否存在
        SpaceUser oldSpaceUser = spaceUserService.getById(sUserId);
        ThrowUtils.throwIf(oldSpaceUser == null, ErrorCode.NOT_FOUND_ERROR);

        boolean result = spaceUserService.removeById(sUserId);

        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 修改空间用户信息
     * @param spaceUser
     * @param request
     * @return
     */
    @PutMapping("/update")
    public BaseResponse<Boolean> updateSpaceUser(@RequestBody SpaceUserEditRequest spaceUser, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUser == null, ErrorCode.PARAMS_ERROR);

        // 判断是否存在
        long id = spaceUser.getId();
        SpaceUser oldSpaceUser = spaceUserService.getById(id);
        ThrowUtils.throwIf(oldSpaceUser == null, ErrorCode.NOT_FOUND_ERROR);

        SpaceUser newSpaceUser = new SpaceUser();
        BeanUtils.copyProperties(spaceUser, newSpaceUser);

        spaceUserService.updateById(newSpaceUser);
        return ResultUtils.success(true);
    }

    /**
     * 获取空间用户信息
     * @param spaceUserQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<SpaceUser> getSpaceUser(SpaceUserQueryRequest spaceUserQueryRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceUserQueryRequest == null, ErrorCode.PARAMS_ERROR);

        Long userId = spaceUserQueryRequest.getUserId();

        Long spaceId = spaceUserQueryRequest.getSpaceId();
        ThrowUtils.throwIf(ObjectUtil.hasEmpty(spaceId, userId), ErrorCode.PARAMS_ERROR);

        // 查询数据库
        SpaceUser spaceUser = spaceUserService.getOne(spaceUserService.getQueryWrapper(spaceUserQueryRequest));
        ThrowUtils.throwIf(spaceUser == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(spaceUser);
    }

    /**
     * 获取空间用户列表
     */
    @GetMapping("/list")
    public BaseResponse<List<SpaceUserVo>> getSpaceUserList(SpaceUserQueryRequest spaceUserQueryRequest, HttpServletRequest request) {


        ThrowUtils.throwIf(spaceUserQueryRequest == null, ErrorCode.PARAMS_ERROR);

     List<SpaceUser> spaceUserList = spaceUserService.list(spaceUserService.getQueryWrapper(spaceUserQueryRequest));
    return ResultUtils.success(spaceUserService.getSpaceUserVOList(spaceUserList));
    }

    /**
     * 获取我加入的空间列表
     * @param request
     * @return
     */
    @GetMapping("/list/my")
    public BaseResponse<List<SpaceUserVo>> getMySpaceUserList(HttpServletRequest request) {

        User loginUser = userService.getLoginUser(request);
        SpaceUserQueryRequest spaceUserQueryRequest = new SpaceUserQueryRequest();
        spaceUserQueryRequest.setUserId(loginUser.getId());
        List<SpaceUser> spaceUserList = spaceUserService.list(
                spaceUserService.getQueryWrapper(spaceUserQueryRequest)
        );
        return ResultUtils.success(spaceUserService.getSpaceUserVOList(spaceUserList));

    }
}

